﻿using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Base;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.RunProperties;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Types;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.WorksheetElements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Samples.Excel
{
    class ConditionalFormattings
    {
        public static void Create()
        {
            Workbook wb = DocumentManager.Instance.CreateSpreadsheet();

            Worksheet sheet1 = wb.CreateWorksheet("Sheet1", null);

            sheet1.CreateCell(1, 1).AddSimpleText("1111");
            sheet1.CreateCell(2, 1).AddSimpleText("2222");
            sheet1.CreateCell(3, 1).AddSimpleText("3333");
            sheet1.CreateCell(4, 1).AddSimpleText("4444");

            ConditionalFormatting cf;
            sheet1.ConditionalFormattings.Add(cf = new ConditionalFormatting()
            {
                SequenceOfReferences = "A1:A4"
            });

            ColorScale cs = new ColorScale();
            cf.Rules.Add(new CfRule()
            {
                Type = ST_CfType.ColorScale,
                Priority = 1,
                ColorScale = cs
            });
            cs.Cfvos.Add(new CFVO()
            {
                Type = ST_CfvoType.Minimum,
                Value = "0"
            });
            cs.Cfvos.Add(new CFVO()
            {
                Type = ST_CfvoType.Percentile,
                Value = "50"
            });
            cs.Cfvos.Add(new CFVO()
            {
                Type = ST_CfvoType.Maximum,
                Value = "0"
            });

            cs.Colors.Add(new Color() { Tint = -0.499984740745262 });
            cs.Colors.Add(new Color() { Tint = -0.249977111117893 });
            cs.Colors.Add(new Color());

            wb.Save(@"samples\conditionalformatting.xlsx");

            Workbook parsed = null;
            if (DocumentManager.Instance.OpenExcelDocument(@"samples\conditionalformatting.xlsx", out parsed))
            {
                Console.WriteLine("Sample created and parsed succesfully!");
            }
            Console.ReadLine();
        }
    }
}
